// Copyright (c) 2024, json_wang and contributors
// For license information, please see license.txt

frappe.ui.form.on("CP_Expense", {
    refresh(frm) {

    },
    onload(frm) {
        //明细行级联选择
        frm.set_query("invoice_no", "child_trip", function (doc, cdt, cdn) {            
            return {
                "filters": {
                    "is_use": ["!=",1]
                }
            };
        });
        //明细行级联选择
        frm.set_query("invoice_no", "child_other", function (doc, cdt, cdn) {            
            return {
                "filters": {
                    "is_use": ["!=",1]
                }
            };
        });
    }
});

frappe.ui.form.on('CP_Expense_Trip',
    { // The child table is defined in a DoctType called "Dynamic Link"
        invoice_type(frm, cdt, cdn) {
            //console.log(frm)
            let row = frm.doc.child_trip.find(f => f.name === cdn)
            row.invoice_no = null
            row.invoice_supplier = null
            row.total_with_tax = null
            frm.refresh_field('child_trip')
        },
        start_time(frm, cdt, cdn) {
            //console.log(frm)
            let row = frm.doc.child_trip.find(f => f.name === cdn)
            //console.log(row)
            row.trip_data = row.start_time.substring(6,13)+'时自'+row.start_city+"出发=>"+row.arrive_time.substring(6,13)+'时到达'+row.arrive_city
            frm.refresh_field('child_trip')
        },
        start_city(frm, cdt, cdn) {
            //console.log(frm)
            let row = frm.doc.child_trip.find(f => f.name === cdn)
            //console.log(row)
            row.trip_data = row.start_time.substring(6,13)+'时自'+row.start_city+"出发=>"+row.arrive_time.substring(6,13)+'时到达'+row.arrive_city
            frm.refresh_field('child_trip')
        },
        arrive_time(frm, cdt, cdn) {
            //console.log(frm)
            let row = frm.doc.child_trip.find(f => f.name === cdn)
            //console.log(row)
            row.trip_data = row.start_time.substring(6,13)+'时自'+row.start_city+"出发=>"+row.arrive_time.substring(6,13)+'时到达'+row.arrive_city
            frm.refresh_field('child_trip')
        },
        arrive_city(frm, cdt, cdn) {
            //console.log(frm)
            let row = frm.doc.child_trip.find(f => f.name === cdn)
            //console.log(row)
            row.trip_data = row.start_time.substring(6,13)+'时自'+row.start_city+"出发=>"+row.arrive_time.substring(6,13)+'时到达'+row.arrive_city
            frm.refresh_field('child_trip')
        },
        make_trip_data(frm, cdt, cdn) {
            //console.log(frm)
            let row = frm.doc.child_trip.find(f => f.name === cdn)
            //console.log(row)
            row.trip_data = row.start_time.substring(6,13)+'时自'+row.start_city+"出发=>"+row.arrive_time.substring(6,13)+'时到达'+row.arrive_city
            frm.refresh_field('child_trip')
        }
    },
);

frappe.ui.form.on('CP_Expense_Other',
    { // The child table is defined in a DoctType called "Dynamic Link"
        invoice_type(frm, cdt, cdn) {
            //console.log(frm)
            let row = frm.doc.child_other.find(f => f.name === cdn)
            row.invoice_no = null
            row.invoice_supplier = null
            row.total_with_tax = null
            frm.refresh_field('child_other')
        }
    },
);